package com.westa.smallshop.common;

import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.westa.smallshop.R;


public class BaseActivity extends AppCompatActivity {


    public static final int RESULT_OK_REFRESH = -1;
    public static final int RESULT_OK_DELETE = -10;
    @Nullable
    private Toolbar mToolbar;

    @Nullable
    private TextView mTvToolbar;

    private long exitTime = 0;

    private boolean hasNavigationIcon = true;

    private boolean isCenterToolbarTitle = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            onInitInstanceStateFromIntent();
        } else {
            onRestoreInstanceStateFromBundle(savedInstanceState);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initToolbar();
        initNavigationIcon();
        setCenterToolbarTitle(isCenterToolbarTitle);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (mTvToolbar != null && isCenterToolbarTitle) {
            mTvToolbar.setText(title);
        }
    }


    @Nullable
    public Toolbar getToolbar() {
        return mToolbar;
    }

    @Override
    public void onBackPressed() {
        if (!onBackOrNavigationPressed()) {
            finish();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home && !onBackOrNavigationPressed()) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Called to pressed back or NavigationIcon.
     *
     * @return boolean Return true if this click event was consumed.
     */
    public boolean onBackOrNavigationPressed() {

        return false;
    }


    public void exitApp() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(this, R.string.exit_app_hint, Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    public void setHasNavigationIcon(boolean hasNavigationIcon) {
        this.hasNavigationIcon = hasNavigationIcon;
        if (getSupportActionBar() != null) {
            if (hasNavigationIcon) {
                initNavigationIcon();
            } else {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            }
        }
    }

    private void initToolbar() {

        mToolbar = (Toolbar) super.findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
    }

    private void initNavigationIcon() {

        if (hasNavigationIcon) {
            Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.ic_ab_back_mtrl_am_alpha);

            if (getSupportActionBar() != null) {
                upArrow.setColorFilter(getResources().getColor(R.color.colorDominantIcon), PorterDuff.Mode.SRC_ATOP);
                getSupportActionBar().setHomeAsUpIndicator(upArrow);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }
    }


    /**
     * @param centerToolbarTitle 是否将toolbar 标题显示在中间
     */
    public void setCenterToolbarTitle(boolean centerToolbarTitle) {
        isCenterToolbarTitle = centerToolbarTitle;
        if (getSupportActionBar() != null) {
            if (mToolbar != null && mTvToolbar == null) {
                mTvToolbar = (TextView) mToolbar.findViewById(R.id.tv_toolbar_title);
            }
            if (mTvToolbar != null) {
                mTvToolbar.setVisibility(isCenterToolbarTitle ? View.VISIBLE : View.GONE);
            }
            getSupportActionBar().setDisplayShowTitleEnabled(!isCenterToolbarTitle);
        } else {
            isCenterToolbarTitle = false;
        }
    }

    protected void onInitInstanceStateFromIntent() {

    }

    protected void onRestoreInstanceStateFromBundle(@NonNull Bundle savedInstanceState) {

    }

}
